﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace Wmi
{
    namespace cimv2
    {

        public class Cimv2ComputerSystem
        {
            #region Declarations
            private static String queryCimv2ComputerSystem = "SELECT * FROM Win32_ComputerSystem";
            private System.Collections.ArrayList Cimv2ComputerSystemObjects;
            private System.Management.SelectQuery sqCimv2ComputerSystem;
            private System.Management.ManagementScope managementScope;
            private System.Management.ManagementObjectSearcher mosCimv2ComputerSystem;
            private System.Management.ManagementObjectCollection mocCimv2ComputerSystem;
            public struct Cimv2Win32ComputerSystem
            {
                public UInt16 AdminPasswordStatus;
                public Boolean AutomaticManagedPagefile;
                public Boolean AutomaticResetBootOption;
                public Boolean AutomaticResetCapability;
                public UInt16 BootOptionOnLimit;
                public UInt16 BootOptionOnWatchDog;
                public Boolean BootROMSupported;
                public String BootupState;
                public String Caption;
                public UInt16 ChassisBootupState;
                public String CreationClassName;
                public Int16 CurrentTimeZone;
                public Boolean DaylightInEffect;
                public String Description;
                public String DNSHostName;
                public String Domain;
                public UInt16 DomainRole;
                public Boolean EnableDaylightSavingsTime;
                public UInt16 FrontPanelResetStatus;
                public Boolean InfraredSupported;
                public String InitialLoadInfo;
                public DateTime InstallDate;
                public UInt16 KeyboardPasswordStatus;
                public String LastLoadInfo;
                public String Manufacturer;
                public String Model;
                public String Name;
                public String NameFormat;
                public Boolean NetworkServerModeEnabled;
                public UInt32 NumberOfLogicalProcessors;
                public UInt32 NumberOfProcessors;
                public UInt16[] OEMLogoBitmap;
                public String[] OEMStringArray;
                public Boolean PartOfDomain;
                public Int64 PauseAfterReset;
                public UInt16 PCSystemType;
                public UInt16[] PowerManagementCapabilities;
                public Boolean PowerManagementSupported;
                public UInt16 PowerOnPasswordStatus;
                public UInt16 PowerState;
                public UInt16 PowerSupplyState;
                public String PrimaryOwnerContact;
                public String PrimaryOwnerName;
                public UInt16 ResetCapability;
                public Int16 ResetCount;
                public Int16 ResetLimit;
                public String[] Roles;
                public String Status;
                public String[] SupportContactDescription;
                public UInt16 SystemStartupDelay;
                public String[] SystemStartupOptions;
                public UInt16 SystemStartupSetting;
                public String SystemType;
                public UInt16 ThermalState;
                public UInt64 TotalPhysicalMemory;
                public String UserName;
                public UInt16 WakeUpType;
                public String Workgroup;
            }
            #endregion 

            public Cimv2ComputerSystem(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqCimv2ComputerSystem = new System.Management.SelectQuery(queryCimv2ComputerSystem);
                mosCimv2ComputerSystem = new ManagementObjectSearcher(managementScope, sqCimv2ComputerSystem);
                mocCimv2ComputerSystem = mosCimv2ComputerSystem.Get();
            }
            /// <summary>
            /// Rename - NOT FINISHED !!!
            /// </summary>
            /// <param name="Name"></param>
            /// <param name="Password"></param>
            /// <param name="UserName"></param>
            /// <returns></returns>
            public Boolean Rename(String Name, String Password, String UserName ) 
            {
                Object returnObj = null;
                Object[] methodArgs = { Name, Password, UserName };

                try
                {
                    foreach (ManagementObject moDfsrConnectionInfo in mocCimv2ComputerSystem)
                    {
                        //returns 0 (zero) if successful, non-zero if not
                        returnObj = moDfsrConnectionInfo.InvokeMethod("Rename", methodArgs);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return false;
                }

                return true;
            }
            /// <summary>
            /// JoinDomainOrWorkgroup - NOT FINISHED !!!
            /// </summary>
            /// <param name="Name"></param>
            /// <param name="Password"></param>
            /// <param name="UserName"></param>
            /// <param name="AccountOU"></param>
            /// <param name="FJoinOptions"></param>
            /// <returns></returns>
            public Boolean JoinDomainOrWorkgroup(String Name, String Password, String UserName, String AccountOU, UInt32 FJoinOptions)
            {
                Object returnObj = null;
                Object[] methodArgs = { Name, Password, UserName, AccountOU, FJoinOptions };

                try
                {
                    foreach (ManagementObject moDfsrConnectionInfo in mocCimv2ComputerSystem)
                    {
                        //returns 0 (zero) if successful, non-zero if not
                        returnObj = moDfsrConnectionInfo.InvokeMethod("JoinDomainOrWorkgroup", methodArgs);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return false;
                }

                return true;
            }
            /// <summary>
            /// UnjoinDomainOrWorkgroup
            /// </summary>
            /// <param name="Password"></param>
            /// <param name="UserName"></param>
            /// <param name="FJoinOptions"></param>
            /// <returns></returns>
            public Boolean UnjoinDomainOrWorkgroup(String Password, String UserName, UInt32 FJoinOptions)
            {
                Object returnObj = null;
                Object[] methodArgs = { Password, UserName, FJoinOptions };

                try
                {
                    foreach (ManagementObject moDfsrConnectionInfo in mocCimv2ComputerSystem)
                    {
                        //returns 0 (zero) if successful, non-zero if not
                        returnObj = moDfsrConnectionInfo.InvokeMethod("UnjoinDomainOrWorkgroup", methodArgs);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return false;
                }

                return true;
            }
            /// <summary>
            /// Get - Not Finished !!!
            /// </summary>
            public void Get()
            {
                Cimv2Win32ComputerSystem _Cimv2Win32ComputerSystem = new Cimv2Win32ComputerSystem();
                try
                {
                    foreach (ManagementObject moCimv2ComputerSystem in mocCimv2ComputerSystem)
                    {
                        if (moCimv2ComputerSystem["Model"] != null)
                        {
                            _Cimv2Win32ComputerSystem.Model = (System.String)moCimv2ComputerSystem["Model"];
                        }
                        if (moCimv2ComputerSystem["Manufacturer"] != null)
                        {
                            _Cimv2Win32ComputerSystem.Manufacturer = (System.String)moCimv2ComputerSystem["Manufacturer"];
                        }
                        if (moCimv2ComputerSystem["TotalPhysicalMemory"] != null)
                        {
                            _Cimv2Win32ComputerSystem.TotalPhysicalMemory = (System.UInt64)moCimv2ComputerSystem["TotalPhysicalMemory"];
                        }
                        if (moCimv2ComputerSystem["NumberOfProcessors"] != null)
                        {
                            _Cimv2Win32ComputerSystem.NumberOfProcessors = (System.UInt32)moCimv2ComputerSystem["NumberOfProcessors"];
                        }
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                }
            }
        }
        public class Cimv2Processor
        {
            private static String queryCimv2Processor = "SELECT * FROM Win32_Processor";
            private System.Collections.ArrayList Cimv2ProcessorObjects;
            private System.Management.SelectQuery sqCimv2Processor;
            private System.Management.ManagementScope managementScope;
            private System.Management.ManagementObjectSearcher mosCimv2Processor;
            private System.Management.ManagementObjectCollection mocCimv2Processor;

            public struct Cimv2Win32Processor
            {
                  public UInt16 AddressWidth;
                  public UInt16 Architecture;
                  public UInt16 Availability;
                  public String Caption;
                  public UInt32 ConfigManagerErrorCode;
                  public Boolean ConfigManagerUserConfig;
                  public UInt16 CpuStatus;
                  public String CreationClassName;
                  public UInt32 CurrentClockSpeed;
                  public UInt16 CurrentVoltage;
                  public UInt16 DataWidth;
                  public String Description;
                  public String DeviceID;
                  public Boolean ErrorCleared;
                  public String ErrorDescription;
                  public UInt32 ExtClock;
                  public UInt16 Family;
                  public DateTime InstallDate;
                  public UInt32 L2CacheSize;
                  public UInt32 L2CacheSpeed;
                  public UInt32 L3CacheSize;
                  public UInt32 L3CacheSpeed;
                  public UInt32 LastErrorCode;
                  public UInt16 Level;
                  public UInt16 LoadPercentage;
                  public String Manufacturer;
                  public UInt32 MaxClockSpeed;
                  public String Name;
                  public UInt32 NumberOfCores;
                  public UInt32 NumberOfLogicalProcessors;
                  public String OtherFamilyDescription;
                  public String PNPDeviceID;
                  public UInt16[] PowerManagementCapabilities;
                  public Boolean PowerManagementSupported;
                  public String ProcessorId;
                  public UInt16 ProcessorType;
                  public UInt16 Revision;
                  public String Role;
                  public String SocketDesignation;
                  public String Status;
                  public UInt16 StatusInfo;
                  public String Stepping;
                  public String SystemCreationClassName;
                  public String SystemName;
                  public String UniqueId;
                  public UInt16 UpgradeMethod;
                  public String Version;
                  public UInt32 VoltageCaps;
            }

            public Cimv2Processor(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqCimv2Processor = new System.Management.SelectQuery(queryCimv2Processor);
                mosCimv2Processor = new ManagementObjectSearcher(managementScope, sqCimv2Processor);
                mocCimv2Processor = mosCimv2Processor.Get();
            }

            public void Get()
            {
                Cimv2Win32Processor _Cimv2Win32Processor = new Cimv2Win32Processor();
                try
                {
                    foreach (ManagementObject moCimv2Processor in mocCimv2Processor)
                    {
                        if (moCimv2Processor["MaxClockSpeed"] != null)
                        {
                            _Cimv2Win32Processor.MaxClockSpeed = (System.UInt32)moCimv2Processor["MaxClockSpeed"];
                        }

                        if(moCimv2Processor["NumberOfCores"] != null ) 
                        {
                            _Cimv2Win32Processor.NumberOfCores = (System.UInt32)moCimv2Processor["NumberOfCores"];
                        }
                        
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                }

            }
        }
        public class Cimv2PhysicalMemory
        {
            private static String queryCimv2PhysicalMemory = "SELECT Capacity FROM Win32_PhysicalMemory";
            private System.Collections.ArrayList Cimv2PhysicalMemoryObjects;
            private System.Management.SelectQuery sqCimv2PhysicalMemory;
            private System.Management.ManagementScope managementScope;
            private System.Management.ManagementObjectSearcher mosCimv2PhysicalMemory;
            private System.Management.ManagementObjectCollection mocCimv2PhysicalMemory;
            

            public struct Cimv2Win32PhysicalMemory
            {
                public String BankLabel;
                public UInt64 Capacity;
                public String Caption;
                public String CreationClassName;
                public UInt16 DataWidth;
                public String Description;
                public String DeviceLocator;
                public UInt16 FormFactor;
                public Boolean HotSwappable;
                public String InstallDate;
                public UInt16 InterleaveDataDepth;
                public UInt32 InterleavePosition;
                public String Manufacturer;
                public UInt16 MemoryType;
                public String Model;
                public String Name;
                public String OtherIdentifyingInfo;
                public String PartNumber;
                public UInt32 PositionInRow;
                public Boolean PoweredOn;
                public Boolean Removable;
                public Boolean Replaceable;
                public String SerialNumber;
                public String SKU;
                public UInt32 Speed;
                public String Status;
                public String Tag;
                public UInt16 TotalWidth;
                public UInt16 TypeDetail;
                public String Version;
            }

            public Cimv2PhysicalMemory(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqCimv2PhysicalMemory = new System.Management.SelectQuery(queryCimv2PhysicalMemory);
                mosCimv2PhysicalMemory = new ManagementObjectSearcher(managementScope, sqCimv2PhysicalMemory);
                mocCimv2PhysicalMemory = mosCimv2PhysicalMemory.Get();
            }
            public void Get()
            {
                Cimv2Win32PhysicalMemory _Cimv2Win32PhysicalMemory = new Cimv2Win32PhysicalMemory();
                try
                {
                    foreach (ManagementObject moCimv2PhysicalMemory in mocCimv2PhysicalMemory)
                    {
                        if (moCimv2PhysicalMemory["Capacity"] != null)
                        {
                            _Cimv2Win32PhysicalMemory.Capacity += (System.UInt64)moCimv2PhysicalMemory["Capacity"];
                        }
                    }
                    _Cimv2Win32PhysicalMemory.Capacity >>= 20;
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                }
                Cimv2PhysicalMemoryObjects.Add(_Cimv2Win32PhysicalMemory);
            }
            public System.Collections.ArrayList Cimv2PhysicalMemoryObjectCollection { get { return Cimv2PhysicalMemoryObjects; } }
        }
        public class Cimv2PerfRawDataPerfDiskPhysicalDisk
        {
            private static String queryCimv2PerfRawDataPerfDiskPhysicalDisk = "SELECT Capacity FROM Win32_PhysicalMemory";
            private System.Collections.ArrayList Cimv2PerfRawDataPerfDiskPhysicalDiskObjects;
            private System.Management.SelectQuery sqCimv2PerfRawDataPerfDiskPhysicalDisk;
            private System.Management.ManagementScope managementScope;
            private System.Management.ManagementObjectSearcher mosCimv2PerfRawDataPerfDiskPhysicalDisk;
            private System.Management.ManagementObjectCollection mocCimv2PerfRawDataPerfDiskPhysicalDisk;
            

            public struct Cimv2PerfRawDataPerfDiskPhysicalDisk
            {
                public String BankLabel;
                public UInt64 Capacity;
                public String Caption;
                public String CreationClassName;
                public UInt16 DataWidth;
                public String Description;
                public String DeviceLocator;
                public UInt16 FormFactor;
                public Boolean HotSwappable;
                public String InstallDate;
                public UInt16 InterleaveDataDepth;
                public UInt32 InterleavePosition;
                public String Manufacturer;
                public UInt16 MemoryType;
                public String Model;
                public String Name;
                public String OtherIdentifyingInfo;
                public String PartNumber;
                public UInt32 PositionInRow;
                public Boolean PoweredOn;
                public Boolean Removable;
                public Boolean Replaceable;
                public String SerialNumber;
                public String SKU;
                public UInt32 Speed;
                public String Status;
                public String Tag;
                public UInt16 TotalWidth;
                public UInt16 TypeDetail;
                public String Version;
            }

            public Cimv2PerfRawDataPerfDiskPhysicalDisk(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqCimv2PerfRawDataPerfDiskPhysicalDisk = new System.Management.SelectQuery(queryCimv2PerfRawDataPerfDiskPhysicalDisk);
                mosCimv2PerfRawDataPerfDiskPhysicalDisk = new ManagementObjectSearcher(managementScope, sqCimv2PerfRawDataPerfDiskPhysicalDisk);
                mocCimv2PerfRawDataPerfDiskPhysicalDisk = mosCimv2PerfRawDataPerfDiskPhysicalDisk.Get();
            }
            public void Get()
            {
                System.UInt64 timeValueA = 0;
                System.UInt64 timeValueB = 0;

                System.UInt64 timeBase = 0;

                System.UInt32 diskReadPerSecA = 0;
                System.UInt32 diskReadPerSecB = 0;

                System.UInt32 diskWritePerSecA = 0;
                System.UInt32 diskWritePerSecB = 0;
                //_DiskWritePerSec = 0;
                //_DiskReadPerSec = 0;

                Cimv2PerfRawDataPerfDiskPhysicalDisk _Cimv2PerfRawDataPerfDiskPhysicalDisk = new Cimv2PerfRawDataPerfDiskPhysicalDisk();
                try
                {
                    foreach (ManagementObject moCimv2PerfRawDataPerfDiskPhysicalDisk in mocCimv2PerfRawDataPerfDiskPhysicalDisk)
                    {
                        if (moCimv2PerfRawDataPerfDiskPhysicalDisk["Timestamp_PerfTime"] != null)
                        {
                            timeValueA = (System.UInt64)moCimv2PerfRawDataPerfDiskPhysicalDisk["Timestamp_PerfTime"];
                        }
                        if (moCimv2PerfRawDataPerfDiskPhysicalDisk["Frequency_PerfTime"] != null)
                        {
                            timeBase = (System.UInt64)moCimv2PerfRawDataPerfDiskPhysicalDisk["Frequency_PerfTime"];
                        }
                        if (moCimv2PerfRawDataPerfDiskPhysicalDisk["DiskReadsPerSec"] != null)
                        {
                            diskReadPerSecA = (System.UInt32)moCimv2PerfRawDataPerfDiskPhysicalDisk["DiskReadsPerSec"];
                        }
                        if (moCimv2PerfRawDataPerfDiskPhysicalDisk["DiskWritesPerSec"] != null)
                        {
                            diskWritePerSecA = (System.UInt32)moCimv2PerfRawDataPerfDiskPhysicalDisk["DiskWritesPerSec"];
                        }
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                }


                for (int count = 0; count < 60; count++)
                {
                    System.Threading.Thread.Sleep(1000);
                    try
                    {
                        sqCimv2PerfRawDataPerfDiskPhysicalDisk 
                            = new System.Management.SelectQuery(queryCimv2PerfRawDataPerfDiskPhysicalDisk);
                        ManagementObjectSearcher mosCimv2PerfRawDataPerfDiskPhysicalDisk 
                            = new ManagementObjectSearcher(mangeScope, sqCimv2PerfRawDataPerfDiskPhysicalDisk);
                        ManagementObjectCollection mocCimv2PerfRawDataPerfDiskPhysicalDisk 
                            = mosCimv2PerfRawDataPerfDiskPhysicalDisk.Get();

                        foreach (ManagementObject moCimv2PerfRawDataPerfDiskPhysicalDisk in mocCimv2PerfRawDataPerfDiskPhysicalDisk)
                        {
                            if (moCimv2PerfRawDataPerfDiskPhysicalDisk["Timestamp_PerfTime"] != null)
                            {
                                timeValueB = (System.UInt64)moCimv2PerfRawDataPerfDiskPhysicalDisk["Timestamp_PerfTime"];
                            }
                            if (moCimv2PerfRawDataPerfDiskPhysicalDisk["DiskReadsPerSec"] != null)
                            {
                                diskReadPerSecB = (System.UInt32)moCimv2PerfRawDataPerfDiskPhysicalDisk["DiskReadsPerSec"];
                            }
                            if (moCimv2PerfRawDataPerfDiskPhysicalDisk["DiskWritesPerSec"] != null)
                            {
                                diskWritePerSecB = (System.UInt32)moCimv2PerfRawDataPerfDiskPhysicalDisk["DiskWritesPerSec"];
                            }
                        }
                        System.UInt32 diffDiskReadPerSec = diskReadPerSecB - diskReadPerSecA;
                        System.UInt32 diffDiskWritePerSec = diskWritePerSecB - diskWritePerSecA;
                        System.UInt64 diffTimeValue = timeValueB - timeValueA;

                        _DiskReadPerSec += ((diskReadPerSecB - diskReadPerSecA) / ((timeValueB - timeValueA) / timeBase));
                        _DiskWritePerSec += ((diskWritePerSecB - diskWritePerSecA) / ((timeValueB - timeValueA) / timeBase));

                        Console.WriteLine("Write: {0}, Read: {1}", (_DiskWritePerSec), (_DiskReadPerSec));
                    }
                    catch (Exception e)
                    {
                        string error = "Disk - Message: " + e.Message + " Source: " + e.Source;
                        Console.Error.WriteLine(error);
                    }
                }
                try
                {
                    System.Double _DiskWritePerSecD = ((System.Double)_DiskWritePerSec / (System.Double)60);
                    System.Double _DiskReadPerSecD = ((System.Double)_DiskReadPerSec / (System.Double)60);

                    _DiskReadPerSec = (System.UInt64)_DiskReadPerSecD;
                    _DiskWritePerSec = (System.UInt64)_DiskWritePerSecD;

                    Console.WriteLine("Final Disk - Write: {0}, Read: {1}", (_DiskWritePerSec), (_DiskReadPerSec));
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                }
                Cimv2PerfRawDataPerfDiskPhysicalDiskObjects.Add(_Cimv2Win32PhysicalMemory);
            }
            public System.Collections.ArrayList Cimv2PhysicalMemoryObjectCollection { get { return Cimv2PhysicalMemoryObjects; } }
        }
        public class Cimv2PerfRawDataPerfOSMemory
        {
            private static String queryCimv2PerfRawDataPerfOSMemory 
                = "SELECT AvailableMBytes, PageFaultsPerSec, PercentCommittedBytesInUse, "
                 + "PercentCommittedBytesInUse_Base, Timestamp_PerfTime,Frequency_PerfTime "
                 + "FROM Win32_PerfRawData_PerfOS_Memory";
            private System.Collections.ArrayList Cimv2PerfRawDataPerfOSMemoryObjects;
            private System.Management.SelectQuery sqCimv2PerfRawDataPerfOSMemory;
            private System.Management.ManagementScope managementScope;
            private System.Management.ManagementObjectSearcher mosCimv2PerfRawDataPerfOSMemory;
            private System.Management.ManagementObjectCollection mocCimv2PerfRawDataPerfOSMemory;


            public struct Cimv2Win32PerfRawDataPerfOSMemory
            {
                public UInt64 AvailableBytes;
                public UInt64 AvailableKBytes;
                public UInt64 AvailableMBytes;
                public UInt64 CacheBytes;
                public UInt64 CacheBytesPeak;
                public UInt32 CacheFaultsPerSec;
                public String Caption;
                public UInt64 CommitLimit;
                public UInt64 CommittedBytes;
                public UInt32 DemandZeroFaultsPerSec;
                public String Description;
                public UInt32 FreeSystemPageTableEntries;
                public UInt64 Frequency_Object;
                public UInt64 Frequency_PerfTime;
                public UInt64 Frequency_Sys100NS;
                public String Name;
                /* Change in Type from UInt32 to UInt64 */
                public UInt64 PageFaultsPerSec;
                public UInt32 PageReadsPerSec;
                public UInt32 PagesInputPerSec;
                public UInt32 PagesOutputPerSec;
                public UInt32 PagesPerSec;
                public UInt32 PageWritesPerSec;
                public UInt32 PercentCommittedBytesInUse;
                public UInt32 PercentCommittedBytesInUse_Base;
                public UInt32 PoolNonpagedAllocs;
                public UInt64 PoolNonpagedBytes;
                public UInt32 PoolPagedAllocs;
                public UInt64 PoolPagedBytes;
                public UInt64 PoolPagedResidentBytes;
                public UInt64 SystemCacheResidentBytes;
                public UInt64 SystemCodeResidentBytes;
                public UInt64 SystemCodeTotalBytes;
                public UInt64 SystemDriverResidentBytes;
                public UInt64 SystemDriverTotalBytes;
                public UInt64 Timestamp_Object;
                public UInt64 Timestamp_PerfTime;
                public UInt64 Timestamp_Sys100NS;
                public UInt32 TransitionFaultsPerSec;
                public UInt32 WriteCopiesPerSec;
            }

            public Cimv2PerfRawDataPerfOSMemory(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqCimv2PerfRawDataPerfOSMemory = new System.Management.SelectQuery(queryCimv2PerfRawDataPerfOSMemory);
                mosCimv2PerfRawDataPerfOSMemory = new ManagementObjectSearcher(managementScope, sqCimv2PerfRawDataPerfOSMemory);
                mocCimv2PerfRawDataPerfOSMemory = mosCimv2PerfRawDataPerfOSMemory.Get();
            }
            public void Get()
            {
                System.UInt32 _PercentCommittedBytesInUse_Base = 0;
                System.UInt64 timeValueA = 0;
                System.UInt64 timeValueB = 0;
                System.UInt64 timeBase = 0;
                System.UInt32 PageFaultsPerSecA = 0;
                System.UInt32 PageFaultsPerSecB = 0;

                Cimv2Win32PerfRawDataPerfOSMemory _cimv2Win32PerfRawDataPerfOSMemory =
                    new Cimv2Win32PerfRawDataPerfOSMemory();

                try
                {
                    foreach (ManagementObject moCimv2PerfRawDataPerfOSMemory in mocCimv2PerfRawDataPerfOSMemory)
                    {

                        if (moCimv2PerfRawDataPerfOSMemory["Timestamp_PerfTime"] != null)
                        {
                            timeValueA = (System.UInt64)moCimv2PerfRawDataPerfOSMemory["Timestamp_PerfTime"];
                        }
                        if (moCimv2PerfRawDataPerfOSMemory["Frequency_PerfTime"] != null)
                        {
                            timeBase = (System.UInt64)moCimv2PerfRawDataPerfOSMemory["Frequency_PerfTime"];
                        }

                        if (moCimv2PerfRawDataPerfOSMemory["AvailableMBytes"] != null)
                        {
                            _cimv2Win32PerfRawDataPerfOSMemory.AvailableMBytes = (System.UInt64)moCimv2PerfRawDataPerfOSMemory["AvailableMBytes"];
                        }
                        if (moCimv2PerfRawDataPerfOSMemory["PageFaultsPerSec"] != null)
                        {
                            PageFaultsPerSecA = (System.UInt32)moCimv2PerfRawDataPerfOSMemory["PageFaultsPerSec"];
                        }
                        if (moCimv2PerfRawDataPerfOSMemory["PercentCommittedBytesInUse_Base"] != null)
                        {
                            _PercentCommittedBytesInUse_Base = (System.UInt32)moCimv2PerfRawDataPerfOSMemory["PercentCommittedBytesInUse_base"];
                            if (moCimv2PerfRawDataPerfOSMemory["PercentCommittedBytesInUse"] != null)
                            {
                                System.UInt32 _PercentCommittedBytesInUseB = (System.UInt32)moCimv2PerfRawDataPerfOSMemory["PercentCommittedBytesInUse"];
                                _cimv2Win32PerfRawDataPerfOSMemory.PercentCommittedBytesInUse
                                    = (100 * _PercentCommittedBytesInUseB) / _PercentCommittedBytesInUse_Base;
                            }
                        }
                    }
                    
                    System.Threading.Thread.Sleep(1000);
                    sqCimv2PerfRawDataPerfOSMemory = new System.Management.SelectQuery(queryCimv2PerfRawDataPerfOSMemory);
                    mosCimv2PerfRawDataPerfOSMemory = new ManagementObjectSearcher(managementScope, sqCimv2PerfRawDataPerfOSMemory);
                    mocCimv2PerfRawDataPerfOSMemory = mosCimv2PerfRawDataPerfOSMemory.Get();

                    foreach (ManagementObject mo in mocCimv2PerfRawDataPerfOSMemory)
                    {

                        if (mo["Timestamp_PerfTime"] != null)
                        {
                            timeValueB = (System.UInt64)mo["Timestamp_PerfTime"];
                        }
                        if (mo["PageFaultsPerSec"] != null)
                        {
                            PageFaultsPerSecB = (System.UInt32)mo["PageFaultsPerSec"];
                        }
                    }

                    System.UInt64 timeValueC = timeValueB - timeValueA;
                    System.UInt32 PageFaultsPerSecC = PageFaultsPerSecB - PageFaultsPerSecA;
                    System.UInt64 timeValueBase = timeValueC / timeBase;

                    _cimv2Win32PerfRawDataPerfOSMemory.PageFaultsPerSec
                        = (System.UInt32)PageFaultsPerSecC / timeValueBase;

                    //Console.WriteLine("Memory - AvailMB: {0}, Commited: {1}%, PageFaults/Sec: {2}", _AvailableMBytes, _PercentCommittedBytesInUse, _PageFaultsPerSec);
                    Cimv2PerfRawDataPerfOSMemoryObjects.Add(_cimv2Win32PerfRawDataPerfOSMemory);
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                }
            }
            public System.Collections.ArrayList Cimv2PerfRawDataPerfOSMemoryObjectCollection { get { return Cimv2PerfRawDataPerfOSMemoryObjects; } }
        }
        public class Cimv2DiskDrive
        {
            private String queryCimv2DiskDrive = "SELECT * FROM Win32_DiskDrive";
            private System.Collections.ArrayList arrayCimv2DiskDrive;
            private System.Management.SelectQuery sqCimv2DiskDrive;
            private System.Management.ManagementScope managementScope;
            private System.Management.ManagementObjectSearcher mosCimv2DiskDrive;
            private System.Management.ManagementObjectCollection mocCimv2DiskDrive;

            public struct DiskDrive
            {
                
                UInt16 Availability;
                UInt32 BytesPerSector;
                UInt16 Capabilities;
                String CapabilityDescriptions;
                String Caption;
                String CompressionMethod;
                UInt32 ConfigManagerErrorCode;
                Boolean ConfigManagerUserConfig;
                String CreationClassName;
                UInt64 DefaultBlockSize;
                String Description;
                String DeviceID;
                Boolean ErrorCleared;
                String ErrorDescription;
                String ErrorMethodology;
                String FirmwareRevision;
                UInt32 Index;
                String InstallDate;
                String InterfaceType;
                UInt32 LastErrorCode;
                String Manufacturer;
                UInt64 MaxBlockSize;
                UInt64 MaxMediaSize;
                Boolean MediaLoaded;
                String MediaType;
                UInt64 MinBlockSize;
                String Model;
                String Name;
                Boolean NeedsCleaning;
                UInt32 NumberOfMediaSupported;
                UInt32 Partitions;
                String PNPDeviceID;
                UInt16 PowerManagementCapabilities;
                Boolean PowerManagementSupported;
                UInt32 SCSIBus;
                UInt16 SCSILogicalUnit;
                UInt16 SCSIPort;
                UInt16 SCSITargetId;
                UInt32 SectorsPerTrack;
                String SerialNumber;
                UInt32 Signature;
                UInt64 Size;
                String Status;
                UInt16 StatusInfo;
                String SystemCreationClassName;
                String SystemName;
                UInt64 TotalCylinders;
                UInt32 TotalHeads;
                UInt64 TotalSectors;
                UInt64 TotalTracks;
                UInt32 TracksPerCylinder;
            }
            public Cimv2DiskDrive(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqCimv2DiskDrive = new System.Management.SelectQuery(queryCimv2DiskDrive);
                mosCimv2DiskDrive = new ManagementObjectSearcher(managementScope, sqCimv2DiskDrive);
                mocCimv2DiskDrive = mosCimv2DiskDrive.Get();
            }
            public void Get()
            {
                try
                {
                    foreach (ManagementObject moCimv2DiskDrive in mocCimv2DiskDrive)
                    {

                        if (moCimv2DiskDrive["Name"] != null)
                        {
                            String name = moCimv2DiskDrive["Name"].ToString();
                            Console.WriteLine(name);
                        }
                        if (moCimv2DiskDrive["Model"] != null)
                        {
                            String name = moCimv2DiskDrive["Model"].ToString();
                            Console.WriteLine(name);
                        }
                        if (moCimv2DiskDrive["Size"] != null)
                        {
                            UInt64 size = (UInt64)moCimv2DiskDrive["Size"];
                            Console.WriteLine(size);
                        }

                    }
                }

                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                }

            }
        }
    }
}
